package com.xiaoq.route.sites.endpoint;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.coms.util.MathUtil;
import com.xiaoq.route.sites.entity.RoutePointSitesEntity;
import com.xiaoq.route.sites.entity.SitesRouteEntity;
import com.xiaoq.route.sites.service.ISitesRouteService2;
import com.xiaoq.route.sites.vo.SitesRouteVo;
import com.xiaoq.sites.entity.SitesEntity;
import com.xiaoq.sites.service.IBannerService;
import com.xiaoq.sites.service.ISitesService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags={"景区路线接口"})
@RestController
@RequestMapping("/api/v1/routes")
public class SitesRouteEndpoint {

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private ISitesRouteService2 iSitesRouteService;

    @Autowired
    private IBannerService bannerSer;

    @Autowired
    private ISitesService sitesService;

    public class SitesRouteListResponse extends GenericListResponse<SitesRouteVo> {}

    public class SitesRouteDataResponse extends GenericDataResponse<SitesRouteVo> {}

    @ApiOperation(value = "添加景区线路", notes = "添加景区线路")
    @ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "添加景区线路") })
    @PostMapping(value = "/sitesRoute", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> addHotel(
            @RequestBody @ApiParam(value = "sitesRouteVo", required = true) SitesRouteVo sitesRouteVo, HttpServletRequest request) {

        SitesRouteEntity sitesRouteEntity = new SitesRouteEntity();
        BeanUtils.copyProperties(sitesRouteVo,sitesRouteEntity);
        sitesRouteEntity.setId(idGenerator.nextId());
        iSitesRouteService.addSitesRoute(sitesRouteEntity);
        return GenericResponse.ok();

    }

    /**
     * 在线路上添加一个新的景点
     * @param routeId
     * @return
     */
    @ApiOperation(value="在线路上添加一个景区", notes="在线路上添加一个景区")
    @ApiResponses({
            @ApiResponse(code=200,response=GenericResponse.class,message="在线路上添加一个景区")
    })
    @PostMapping(value="/sites_route/{routeId}/sites_points2", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> addRouteSitesPoint(
            @PathVariable(value="routeId",required=true)@ApiParam(value="routeId",required=true)String routeId,
            @RequestParam(value="sitesId",required=true)@ApiParam(value="景区Id",required=true)String sitesId,
            @RequestParam(value="sortNum",required=true)@ApiParam(value="sortNum(插入景点的前一个景点的序号如果是第一个景点传0)",required=true)Integer sortNum,
            @RequestParam(value="memo",required=false)@ApiParam(value="memo",required=false)String memo){


        SitesRouteEntity sitesRoute =  iSitesRouteService.findSitesRouteDetail(routeId);
        String zoneCity = sitesRoute.getZoneCity();
        SitesEntity sites = sitesService.findById(sitesId);

        RoutePointSitesEntity routePoint = new RoutePointSitesEntity();

        routePoint.setId(idGenerator.nextId());
        routePoint.setRouteId(routeId);
        routePoint.setSitesId(sitesId);
        routePoint.setSortNum(sortNum+1);
        iSitesRouteService.savePointRoute(routePoint);
        return GenericResponse.ok();

    }


    @ApiOperation(value = "修改景区线路", notes = "修改景区线路")
    @ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "修改景区线路") })
    @PutMapping(value = "/sitesRoute", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> updateHotel(
            @RequestBody @ApiParam(value = "sitesRouteVo", required = true) SitesRouteVo sitesRouteVo, HttpServletRequest request) {

        SitesRouteEntity sitesRouteEntity = new SitesRouteEntity();
        BeanUtils.copyProperties(sitesRouteVo,sitesRouteEntity);
        iSitesRouteService.addSitesRoute(sitesRouteEntity);
        return GenericResponse.ok();

    }

    @ApiOperation(value = "删除景区线路", notes = "删除景区线路")
    @ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "删除景区线路") })
    @DeleteMapping(value = "/sitesRoute", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> deleteRouteSite(
            @RequestParam(value = "id") String id, HttpServletRequest request) {

        iSitesRouteService.deleteSitesRoute(id);
        return GenericResponse.ok();

    }

    @ApiOperation(value = "删除线路上的景区", notes = "删除线路上的景区")
    @ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "删除线路上的景区") })
    @DeleteMapping(value = "/sitesRoute2Sites", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> deleteHotel(
            @RequestParam(value = "id") String id,@RequestParam(value = "sitesId") String sitesId, HttpServletRequest request) {

        iSitesRouteService.deleteRouteSites(id,sitesId);
        return GenericResponse.ok();

    }

    /**
     *
     * @param routeId
     * @return
     */
    @ApiOperation(value="线路上移动景区位置", notes="线路上移动景区位置")
    @ApiResponses({
            @ApiResponse(code=200,response=GenericResponse.class,message="线路上移动景区位置")
    })
    @PutMapping(value="/sites_route/{routeId}/sites_points", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> changeRouteSitesPoint(
            @PathVariable(value="routeId",required=true)@ApiParam(value="routeId",required=true)String routeId,
            @RequestParam(value="sitesId",required=true)@ApiParam(value="要移动景区Id",required=true)String sitesId,
            @RequestParam(value="status",required=true)@ApiParam(value="上移为1下移为0",required=true)Integer status){

        iSitesRouteService.changeSites(routeId,sitesId,status);
        return GenericResponse.ok();

    }

    @ApiOperation(value = "景区线路列表", notes = "景区线路列表")
    @ApiResponses({ @ApiResponse(code = 200, response = SitesRouteListResponse.class, message = "景区线路列表") })
    @GetMapping(value = "/sites", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> findHotels(
            @RequestParam(value = "cityCode") @ApiParam(value = "城市编码") String cityCode, HttpServletRequest request) {

        List<SitesRouteEntity> sitesRoutes =  iSitesRouteService.findSitesRoute(cityCode);
        List<SitesRouteVo> sitesRouteVos = new ArrayList<>();
        SitesRouteVo sitesRouteVo = null;
        for(SitesRouteEntity sites:sitesRoutes){

            String id = sites.getId();
            sitesRouteVo = new SitesRouteVo();
            BeanUtils.copyProperties(sites,sitesRouteVo);
            List<SitesEntity> sitesList = sitesRouteVo.getSitesList();
            if(sitesList==null||sitesList.size()<1){
                sitesList = new ArrayList<>();
                List<RoutePointSitesEntity> routePoints =  iSitesRouteService.findPointRoute(id);
                for(RoutePointSitesEntity routePoint:routePoints){
                    SitesEntity sitesEntity = sitesService.findById(routePoint.getSitesId());
                    sitesList.add(sitesEntity);
                }

                sitesRouteVo.setSitesList(sitesList);
            }

            sitesRouteVos.add(sitesRouteVo);
        }

        return SitesRouteListResponse.listNoCount(sitesRouteVos);

    }

    @ApiOperation(value = "景区线路详情", notes = "景区线路详情")
    @ApiResponses({ @ApiResponse(code = 200, response = SitesRouteDataResponse.class, message = "景区线路详情") })
    @GetMapping(value = "/detail", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
    public ResponseEntity<?> detail(
            @RequestParam(value = "id") @ApiParam(value = "线路Id") String id, HttpServletRequest request) {

        SitesRouteEntity sitesRoute =  iSitesRouteService.findSitesRouteDetail(id);
        SitesRouteVo sitesRouteVo = new SitesRouteVo();
        BeanUtils.copyProperties(sitesRoute,sitesRouteVo);

        List<SitesEntity> sitesList = sitesRouteVo.getSitesList();
        if(sitesList==null||sitesList.size()<1){
            sitesList = new ArrayList<>();
            List<RoutePointSitesEntity> routePoints =  iSitesRouteService.findPointRoute(id);
            for(RoutePointSitesEntity routePoint:routePoints){
                SitesEntity sites = sitesService.findById(routePoint.getSitesId());
                sitesList.add(sites);
            }
        }
        if(sitesList!=null&&sitesList.size()>1){
            for(int i = 0;i<sitesList.size();i++){
                if(i>0){
                    Double lastLongitude = sitesList.get(i-1).getCenterLongitude();
                    Double lastLatitude = sitesList.get(i-1).getCenterLatitude();
                    Double thisLongitude = sitesList.get(i).getCenterLongitude();
                    Double thisLatitude = sitesList.get(i).getCenterLatitude();
                    Double distance = MathUtil.distance(lastLongitude, lastLatitude, thisLongitude, thisLatitude);

                    String distanceString = MathUtil.distance(distance);
                    sitesList.get(i).setLastDistence(distance);
                    sitesList.get(i).setLastDistenceStrng(distanceString);
                }

            }

        }
        sitesRouteVo.setSitesList(sitesList);

        return SitesRouteDataResponse.okWithData(sitesRouteVo);

    }
}
